<?php
//后台登陆控制
include_once('admin_global.php');
$db->Get_user_shell_check($uid, $shell);   //检测用户是否登录
$db->Get_user_type_check($uid, $shell);    //检测用户权限
//查询出已有栏目类别，将其存入数组
$query=$db->select("category","*","");
 //将查询结果存储到数组中
 while($row=$db->fetch_array()){
    $data[]=$row;
}
//获取该栏目的父级栏目ID
$id = $_GET['id']; 
$query=$db->select("category","*","id=$id");
while ($row = $db->fetch_array()) {
    $catePID = $row['catePID'];        //取得该栏目的父类栏目ID
}
$tree = new Tree($data);       //树形类实例化
$gettree = $tree->get_tree(0, "<option value=\$id \$selected>\$spacer\$cateName</option>", $catePID);      //取得带有当前栏目位置的树形结构

//查询出栏目显示风格
$query=$db->select("catestyle","*","isEnabled=1");
 //将查询结果存储到数组中
 while($row=$db->fetch_array()){
    $styleResult[] = $row;
}

//获取当前栏目相关信息，并将内容显示在表单中
$query=$db->select("category","*","id=$id");
while ($row = $db->fetch_array()) {
    $rs = $row;
}

//修改栏目分类
if(isset($_POST['editCate'])){
    //同级栏目名称不能重复
    $catePID=$_POST['pid'];
    $cateName=trim($_POST['cateName']);
    $query=$db->select('category',"*","cateName='$cateName' AND catePID=$catePID");
    $num=$db->db_num_rows();
    if($num>0){
        echo "<script>alert('该栏目已存在！');history.go(-1);</script>";
    }else{
        //一级导航栏目不能是单页栏目
        $cateStyleID = $_POST['cateStyleID'];
        if ($catePID == 0 && $cateStyleID == 2){
            echo "<script>alert('一级导航栏目不能作为单页栏目！');history.go(-1);</script>";
        }else{
            //父栏目不能为单页栏目
            $catePID=$_POST['pid'];
            $query=$db->select('category',"*","id=$catePID");
            while ($row = $db->fetch_array()) {
                $parentCateStyleID = $row['cateStyleID'];
            }
            if ($parentCateStyleID == 2){
                echo "<script>alert('父栏目不能为单页栏目！');history.go(-1);</script>";
            }else{
                //有子栏目的不能设置为单页栏目
                $cateStyleID = $_POST['cateStyleID'];
                if ($cateStyleID == 2){
                    $query=$db->select('category',"*","cateName='$cateName' AND catePID=$id");
                    $num=$db->db_num_rows();
                    if($num>0){
                        echo "<script>alert('含有子栏目的不能设为单页栏目！');history.go(-1);</script>";
                    }
                    //获取表单数据，准备修改数据库
                    if (!empty($_POST['isShow'])) {
                        $isShow = 1;
                    } else {
                        $isShow = 0;
                    }
                    $cateNav = $_POST['cateNav'];
                    $cateIndex=intval($_POST['cateIndex']);
                    $cateRemark = $_POST['cateRemark'];
                    $rows=$db->update("category","catePID=$catePID,cateName='$cateName',cateStyleID=$cateStyleID,cateIndex=$cateIndex,cateNavigation='$cateNav',,isShow=$isShow,cateRemark='$cateRemark'","id=$id");
                    if($rows>0){
                        echo "<script>alert('栏目修改成功！');window.location.href='listCate.php'</script>";
                    } else {
                        echo "<script>alert('栏目修改失败！');history.go(-1);</script>";
                    }
                }else{
                    //获取表单数据，准备修改数据库
                    if (!empty($_POST['isShow'])) {
                        $isShow = 1;
                    } else {
                        $isShow = 0;
                    }
                    $cateNav = $_POST['cateNav'];
                    $cateIndex=intval($_POST['cateIndex']);
                    $cateRemark = $_POST['cateRemark'];
                    $rows=$db->update("category","catePID=$catePID,cateName='$cateName',cateStyleID=$cateStyleID,cateIndex=$cateIndex,cateNavigation='$cateNav',isShow=$isShow,cateRemark='$cateRemark'","id=$id");
                    if($rows>0){
                        echo "<script>alert('栏目修改成功！');window.location.href='listCate.php'</script>";
                    } else {
                        echo "<script>alert('栏目修改失败！');history.go(-1);</script>";
                    }
                }
                
            }

        }
    }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>修改栏目</title>
        <link rel="stylesheet" href="css/right.css" type="text/css" /> 
        <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
        <script type="text/javascript" src="js/jquery.validate-1.13.1.js"></script>
    </head>
    <script>
        $(function(){
            $("#form1").validate({
                rules:{
                    cateName:{
                        required:true,
                    },
                    cateIndex:{
                        required:true,
                        number:true,
                        range:[0,99]
                    }
                },
                messages:{
                    cateName:{
                        required:"请填写栏目名称！",
                    },
                    cateIndex:{
                        required: "请填写栏目的排序值！",
                        number: "栏目排序要求是数值！",
                        range: "栏目排序数值的范围在0~99之间！"
                    }
                }
            })
        })
    </script>
    <body>
        <div id="right_main">
            <div class="right_content">
                <div class="right_top_menu">
                    <h3>
                        您当前的位置：后台 > 栏目管理 > 修改栏目
                    </h3>
                </div>
                <div class="right_center_content">
                    <form id="form1" method="post" action="">
                        <table cellpadding="0" cellspacing="0" border="0" width="100%">
                            <tr>
                                <td valign="top" >
                                    <div class="txl_search"></div>
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%"  class="sy_table">
                                        <tr>
                                            <th colspan="2" >修改栏目分类</th>
                                        </tr>
                                        <tr>
                                            <td width="35%" align="right">所属栏目：</td>
                                            <td width="65%">
                                                <select name="pid">
                                                    <option value="0" >无（作为一级栏目）</option> 
                                                    <?php print_r($gettree); ?>
                                                </select>
                                            </td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">栏目名称：</td>
                                            <td width="65%"><input type="text" name="cateName"  class="int_text" value="<?php echo $rs['cateName']; ?>" size="40" /></td>
                                        </tr>
                                        <tr>
                                            <td width="35%" align="right">栏目说明：</td>
                                            <td width="65%"><input type="text" name="cateRemark"  class="int_text" value="<?php echo $rs['cateRemark']; ?>" size="40" />&nbsp;&nbsp;可以不填</td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">栏目风格：</td>
                                            <td width="65%">
                                                <select name="cateStyleID">
                                                <?php foreach ($styleResult as $row){; ?>
                                                    <option value="<?php echo $row['cateStyleID'] ?>" <?php if($row['cateStyleID']==$rs['cateStyleID']){ echo "selected='selected'"; }?>><?php echo $row['cateStyleName'] ?></option> 
                                                <?php }; ?>
                                                </select> * 栏目风格是指栏目在前台页面显示时的外观样式，默认为文章列表样式，单页栏目请选择“单页内容”，教师风采等以图片为主的栏目请选择图片列表。
                                            </td>
                                        </tr>
                                        <tr>
                                            <td width="35%" align="right">栏目位置：</td>
                                            <td width="65%">
                                                <select name="cateNav">
                                                    <option value="主导航" <?php if ($rs['cateNavigation'] == "主导航") {echo "selected='selected'";}?>>主导航</option> 
                                                    <option value="侧导航" <?php if ($rs['cateNavigation'] == "侧导航") {echo "selected='selected'";}?>>侧导航</option> 
                                                </select>
                                            </td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">栏目排序：</td>
                                            <td width="65%"><input type="text" name="cateIndex"  class="int_text" value="<?php echo $rs['cateIndex']; ?>" size="10" /> &nbsp;值为0-99，值越大，越靠前</td>
                                        </tr>   
                                        <tr>
                                            <td width="35%" align="right">是否显示：</td>
                                            <td width="65%"><input type="checkbox" name="isShow"  class="int_text"  <?php if ($rs['isShow'] == 1) {echo "checked='checked'";} ?> value="6" size="40" /></td>
                                        </tr> 
                                        <tr>
                                            <td colspan="2" align="center" height='30'>
                                                <input type="submit" class="btn_ipt01" name="editCate" value=" 确认修改 "/>
                                            </td>  
                                        </tr>
                                    </table>
                                </td>
                            </tr>   
                        </table>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>
